package cn.duli.myvideo.controller;

import cn.duli.myvideo.pojo.MyResult;
import cn.duli.myvideo.pojo.User;
import cn.duli.myvideo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询所有用户信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    public PageInfo<User> queryLike(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userService.findAll());
        return pageInfo;
    }

    /**
     * 根据用户id删除此用户（status=0）
     * @param uId
     */
    @RequestMapping("/deleteById/{uId}")
    public void deleteById(@PathVariable("uId") Integer uId){
        userService.deleteById(uId);
    }

    /**
     * 注册前，根据用户名判断用户名是否已存在
     * @param uName
     * @return
     */
    @RequestMapping("/registerUsername/{uName}")
    public boolean registerUsername(@PathVariable("uName") String uName){
        boolean result = userService.findUserByUserName(uName);
        return result;
    }

    /**
     * 注册用户
     * @param user
     */
    @PostMapping("/save")
    public MyResult save(@RequestBody User user){
        userService.save(user);
        MyResult myResult = new MyResult();
        myResult.setMsg("注册成功");
        return myResult;
    }

    /**
     * 用户登录
     */
    @RequestMapping("/login")
    public MyResult login(@RequestBody User user){
        User userLogin = userService.login(user);
        System.out.println(userLogin);
        if(userLogin != null){
            MyResult myResult = new MyResult();
            myResult.setCode(200);
            myResult.setMsg("登录成功");
            myResult.setObject(userLogin);
            return myResult;
        }else{
            MyResult myResult = new MyResult();
            myResult.setCode(400);
            myResult.setMsg("手机号或者密码错误");
            myResult.setObject(user);
            return myResult;
        }
    }

    /**
     * 根据用户名回显用户信息
     * @param uName
     * @return
     */
    @GetMapping("/findByUserName/{uName}")
    public MyResult findByUserName(@PathVariable("uName") String uName){
        User user = userService.findByUserName(uName);
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("显示用户信息成功");
        myResult.setObject(user);
        return myResult;
    }

    /**
     * 更改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public MyResult update(@RequestBody User user){
        userService.update(user);
        User user1 = userService.findByUserName(user.getuName());
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("修改成功");
        myResult.setObject(user1);
        return myResult;
    }

    /**
     *用户修改密码时根据用户名查询原密码
     * @param uName
     * @return
     */
    @RequestMapping("/findPwdByUserName/{uName}")
    public String findPwdByUserName(@PathVariable("uName") String uName){
        User user = userService.findPwdByUserName(uName);
        return user.getuPass();
    }

    /**
     * 修改用户密码
     * @param user
     * @param newPwd
     * @return
     */
    @RequestMapping("/updatePwd")
    public MyResult updatePwd(@RequestBody User user,String newPwd){
        User user1 = userService.findByUserName(user.getuName());
        user1.setuPass(newPwd);
        userService.updatePwd(user1);
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("恭喜您，密码修改成功！");
        myResult.setObject(user1);
        return myResult;
    }
}
